package com.laicunba.ext.activity.act20170424;

import java.util.Date;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.laicunba.event.ActivityJoinEvent;
import com.laicunba.pojo.ActivityDO;
import com.laicunba.pojo.ActivityJoinDO;
import com.laicunba.pojo.CashCouponDefDO;
import com.laicunba.pojo.RateIncrCouponDefDO;
import com.laicunba.pojo.RegistryDO;
import com.laicunba.service.ActivityJoinService;
import com.laicunba.service.ActivityService;
import com.laicunba.service.CashCouponDefService;
import com.laicunba.service.RateIncrCouponDefService;
import com.laicunba.service.RegistryService;
import com.laicunba.service.VcoinWalletService;
import com.laicunba.util.AssertUtil;
import com.laicunba.util.CacheUtil;
import com.laicunba.util.DateUtil;
import com.laicunba.util.StringUtil;

//@Component
public class Act20170424Listener implements ApplicationListener<ActivityJoinEvent> {
	@Autowired
	private VcoinWalletService vcoinWalletService;
	@Autowired
	private CashCouponDefService cashCouponDefService;
	@Autowired
	private RateIncrCouponDefService rateIncrCouponDefService;
	@Autowired
	private ActivityJoinService activityJoinService;
	@Autowired
	private RegistryService registryService;
	@Autowired
	private ActivityService activityService;
	
	private static int HOUR24 = 3600 * 24;

	@Override
	public void onApplicationEvent(ActivityJoinEvent event) {
		ActivityJoinDO joinDO = (ActivityJoinDO) event.getSource();
		if (!"20170424".equalsIgnoreCase(joinDO.getActivityId())) {
			return;
		}
		
		ActivityDO activity = activityService.get(ActivityDO.class, joinDO.getActivityId());
		if(activity == null || activity.getBeginTime().after(new Date()) || activity.getEndTime().before(new Date())) {
			return;
		}

		if (joinDO != null && StringUtil.isNotEmpty(joinDO.getUserId(), joinDO.getSubActId())) {
			if ("001".equals(joinDO.getSubActId())) {
				joinDO.setRewardResp("屠龙活动，积分兑换750元京东E卡");
				vcoinWalletService.subtract(joinDO.getUserId(), 50000, "KILL_THE_DRAGON", "屠龙活动兑换奖品扣除积分");
				addHitChance(joinDO.getUserId());
				addKillDragonProgress(joinDO.getUserId(), 100);
			} else if ("002".equals(joinDO.getSubActId())) {
				CashCouponDefDO couponDef = cashCouponDefService.get(CashCouponDefDO.class, "act-kill-the-dragon1-red-package-760");
				if (couponDef != null) {
					cashCouponDefService.send2UserWithMessage(couponDef.getId(), joinDO.getUserId(), false,"屠龙活动积分兑换现金券");
					joinDO.setRewardResp("屠龙活动，积分兑换760元现金券");
					joinDO.setStatus("success");
					vcoinWalletService.subtract(joinDO.getUserId(), 50000, "KILL_THE_DRAGON", "屠龙活动兑换奖品扣除积分");
					addHitChance(joinDO.getUserId());
					addKillDragonProgress(joinDO.getUserId(), 100);
				}
			} else if ("003".equals(joinDO.getSubActId())) {
				joinDO.setRewardResp("屠龙活动，积分兑换300元京东E卡");
				vcoinWalletService.subtract(joinDO.getUserId(), 25000, "KILL_THE_DRAGON", "屠龙活动兑换奖品扣除积分");
				addHitChance(joinDO.getUserId());
				addKillDragonProgress(joinDO.getUserId(), 80);
			} else if ("004".equals(joinDO.getSubActId())) {
				CashCouponDefDO couponDef = cashCouponDefService.get(CashCouponDefDO.class, "act-kill-the-dragon1-red-package-310");
				if (couponDef != null) {
					cashCouponDefService.send2UserWithMessage(couponDef.getId(), joinDO.getUserId(), false,"屠龙活动积分兑换现金券");
					joinDO.setRewardResp("屠龙活动，积分兑换310元现金券");
					joinDO.setStatus("success");
					vcoinWalletService.subtract(joinDO.getUserId(), 25000, "KILL_THE_DRAGON", "屠龙活动兑换奖品扣除积分");
					addHitChance(joinDO.getUserId());
					addKillDragonProgress(joinDO.getUserId(), 80);
				}
			} else if ("005".equals(joinDO.getSubActId())) {
				joinDO.setRewardResp("屠龙活动，积分兑换100元京东E卡");
				vcoinWalletService.subtract(joinDO.getUserId(), 10000, "KILL_THE_DRAGON", "屠龙活动兑换奖品扣除积分");
				addHitChance(joinDO.getUserId());
				addKillDragonProgress(joinDO.getUserId(), 70);
			} else if ("006".equals(joinDO.getSubActId())) {
				CashCouponDefDO couponDef = cashCouponDefService.get(CashCouponDefDO.class, "act-kill-the-dragon1-red-package-110");
				if (couponDef != null) {
					cashCouponDefService.send2UserWithMessage(couponDef.getId(), joinDO.getUserId(), false,"屠龙活动积分兑换现金券");
					joinDO.setRewardResp("屠龙活动，积分兑换110元现金券");
					joinDO.setStatus("success");
					vcoinWalletService.subtract(joinDO.getUserId(), 10000, "KILL_THE_DRAGON", "屠龙活动兑换奖品扣除积分");
					addHitChance(joinDO.getUserId());
					addKillDragonProgress(joinDO.getUserId(), 70);
				}
			} else if ("007".equals(joinDO.getSubActId())) {
				joinDO.setRewardResp("屠龙活动，积分兑换50元京东E卡");
				vcoinWalletService.subtract(joinDO.getUserId(), 5888, "KILL_THE_DRAGON", "屠龙活动兑换奖品扣除积分");
				addHitChance(joinDO.getUserId());
				addKillDragonProgress(joinDO.getUserId(), 60);
			} else if ("008".equals(joinDO.getSubActId())) {
				CashCouponDefDO couponDef = cashCouponDefService.get(CashCouponDefDO.class, "act-kill-the-dragon1-red-package-55");
				if (couponDef != null) {
					cashCouponDefService.send2UserWithMessage(couponDef.getId(), joinDO.getUserId(), false,"屠龙活动积分兑换现金券");
					joinDO.setRewardResp("屠龙活动，积分兑换55元现金券");
					joinDO.setStatus("success");
					vcoinWalletService.subtract(joinDO.getUserId(), 5888, "KILL_THE_DRAGON", "屠龙活动兑换奖品扣除积分");
					addHitChance(joinDO.getUserId());
					addKillDragonProgress(joinDO.getUserId(), 60);
				}
			} else if ("009".equals(joinDO.getSubActId())) {
				joinDO.setRewardResp("屠龙活动，积分兑换15元话费");
				vcoinWalletService.subtract(joinDO.getUserId(), 2200, "KILL_THE_DRAGON", "屠龙活动兑换奖品扣除积分");
				addHitChance(joinDO.getUserId());
				addKillDragonProgress(joinDO.getUserId(), 40);
			} else if ("010".equals(joinDO.getSubActId())) {
				CashCouponDefDO couponDef = cashCouponDefService.get(CashCouponDefDO.class, "act-kill-the-dragon1-red-package-20");
				if (couponDef != null) {
					cashCouponDefService.send2UserWithMessage(couponDef.getId(), joinDO.getUserId(), false,"屠龙活动积分兑换现金券");
					joinDO.setRewardResp("屠龙活动，积分兑换20元现金券");
					joinDO.setStatus("success");
					vcoinWalletService.subtract(joinDO.getUserId(), 2200, "KILL_THE_DRAGON", "屠龙活动兑换奖品扣除积分");
					addHitChance(joinDO.getUserId());
					addKillDragonProgress(joinDO.getUserId(), 40);
				}
			} else if ("011".equals(joinDO.getSubActId())) {
				RateIncrCouponDefDO rateIncrDef = rateIncrCouponDefService.get(RateIncrCouponDefDO.class, "act-kill-the-dragon1-rate-incr-0.5");
				if (rateIncrDef != null) {
					rateIncrCouponDefService.send2User(rateIncrDef.getId(), joinDO.getUserId(), false, "屠龙活动积分兑换加息券");
					joinDO.setRewardResp("屠龙活动，积分兑换0.5%加息券");
					joinDO.setStatus("success");
					vcoinWalletService.subtract(joinDO.getUserId(), 300, "KILL_THE_DRAGON", "屠龙活动兑换奖品扣除积分");
					addHitChance(joinDO.getUserId());
					addKillDragonProgress(joinDO.getUserId(), 10);
				}
			} else if ("012".equals(joinDO.getSubActId())) {
				CashCouponDefDO couponDef = cashCouponDefService.get(CashCouponDefDO.class, "act-kill-the-dragon1-red-package-5");
				if (couponDef != null) {
					cashCouponDefService.send2UserWithMessage(couponDef.getId(), joinDO.getUserId(), false,"屠龙活动积分兑换现金券");
					joinDO.setRewardResp("屠龙活动，积分兑换5元现金券");
					joinDO.setStatus("success");
					vcoinWalletService.subtract(joinDO.getUserId(), 300, "KILL_THE_DRAGON", "屠龙活动兑换奖品扣除积分");
					addHitChance(joinDO.getUserId());
					addKillDragonProgress(joinDO.getUserId(), 10);
				}
			} else if ("013".equals(joinDO.getSubActId())) {
				//A:10元现金券；B:5元现金券；C：0.3%加息券；D:0.1%加息券
				char rewardResp = (char) (new Random().nextInt(4) + 65);
				String couponId = "";
				switch (rewardResp) {
					case 'A':
						couponId = "act-kill-the-dragon2-red-package-10";
						break;
					case 'B':
						couponId = "act-kill-the-dragon2-red-package-5";
						break;
					case 'C':
						couponId = "act-kill-the-dragon2-rate-incr-0.3";
						break;
					case 'D':
						couponId = "act-kill-the-dragon2-rate-incr-0.1";
						break;
					default:
						couponId = "";
						break;
				}
				if(rewardResp == 'A' || rewardResp == 'B') {
					cashCouponDefService.send2UserWithMessage(couponId, joinDO.getUserId(), false,"屠龙活动积分兑换现金券");
				} else if(rewardResp == 'C' || rewardResp == 'D') {
					rateIncrCouponDefService.send2User(couponId, joinDO.getUserId(), false, "屠龙活动积分兑换加息券");
				}
				joinDO.setRewardResp(String.valueOf(rewardResp));
				joinDO.setStatus("success");
				addHitCount(joinDO.getUserId());
			}
			AssertUtil.assertTrue(activityJoinService.updateSkipCheckOwner(joinDO), "REWARD_FAIL");
		}
	}
	
	public void addHitChance(String userId) {
		String chanceKey = "act.20170424chance." + userId + DateUtil.formatDate(new Date());
		RegistryDO registry = registryService.get(RegistryDO.class, "act.20170424." + userId + DateUtil.formatDate(new Date()));
		if(registry == null) {
			if (StringUtil.isEmpty(CacheUtil.getAttribute(chanceKey))) {
				CacheUtil.setAttribute(chanceKey, "1", HOUR24);
			} else {
				CacheUtil.incr(chanceKey, HOUR24);
				CacheUtil.setAttribute("act.20170424CJ." + userId + DateUtil.formatDate(new Date()), "0", HOUR24);
			}
			registry = new RegistryDO();
			registry.setId("act.20170424." + userId + DateUtil.formatDate(new Date()));
			registry.setVal("1");
			registry.setVisible(true);
			registryService.create(registry);
		}
	}
	
	public void addHitCount(String userId) {
		String cjCountKey = "act.20170424CJ." + userId + DateUtil.formatDate(new Date());
		if (StringUtil.isEmpty(CacheUtil.getAttribute(cjCountKey))) {
			CacheUtil.setAttribute(cjCountKey, "1", HOUR24);
		} else {
			CacheUtil.incr(cjCountKey, HOUR24);
		}
	}
	
	public void addKillDragonProgress(String userId, int killProgress) {
		RegistryDO registry = registryService.get(RegistryDO.class, "act.kill-dragon." + userId + ".progress");
		if(registry != null) {
			int totalProgress = Integer.valueOf(registry.getVal()) + killProgress;
			registry.setVal(String.valueOf(totalProgress));
			registryService.updateSkipCheckOwner(registry);
		} else {
			registry = new RegistryDO();
			registry.setId("act.kill-dragon." + userId + ".progress");
			registry.setVal(String.valueOf(killProgress));
			registry.setVisible(true);
			registryService.create(registry);
		}
	}

}
